/*set working directory*/
/*ssc install blindschemes*/

/*Figure 3.1: The Total Number of INGOs in the World, 1900-2022*/
clear
import delimited "Data/wingo.csv"
rename name Name
rename founded Founded
rename country CountryTerritoryHQ
generate wingo=1
keep Name Founded CountryTerritoryHQ wingo
tempfile wingos
save `wingos'
clear
import excel "Data/NGOs_AllCountries", sheet("Sheet1") firstrow
keep Name Founded CountryTerritoryHQ
generate wingo=0
append using `wingos'
drop if Founded==.
foreach num of numlist 1900/2022 {
	gen v_`num'=`num'
	} 
foreach x of varlist v_1900-v_2022 {
    replace `x'=. if `x'<Founded
    }
foreach x of varlist v_1900-v_2022 {
    replace `x'=0 if `x'==.
    }
foreach x of varlist v_1900-v_2022 {
    replace `x'=1 if `x'>0
    }
gen id=_n
reshape long v_, i(id) j(year)	
drop id
rename v_ total
collapse (sum) total, by(year)
label variable total "Total Number"
label variable year "Year"
line total year, xlabel(1900(20)2020) scale(1.7) xtitle("") yla(, format(%12.0fc)) scheme(plotplain) lwidth(medthick)
graph export "Figures/Figure3-1v2.pdf", as(pdf) replace

/*Figure 3.2: The Number of INGO Foundings per Year, United States, 1992-2020*/
/*3.2a*/
clear
import delimited "Data/wingo.csv"
replace country="USA" if country=="United States" | country=="United States of America"
rename name Name
rename founded Founded
rename country CountryTerritoryHQ
generate wingo=1
keep Name Founded CountryTerritoryHQ wingo
tempfile wingos
save `wingos'
clear
import excel "Data/NGOs_AllCountries", sheet("Sheet1") firstrow
keep Name Founded CountryTerritoryHQ
generate wingo=0
append using `wingos'
keep if CountryTerritoryHQ=="USA"
gen count=1
collapse (sum) count, by(Founded)
label variable count "Foundings"
label variable Founded "Year"
rename Founded year
tempfile usafoundings
save `usafoundings'
clear
import excel "Data/gallup-trend.xlsx", sheet("Sheet0") firstrow
rename NormalizedResponseCategories active
rename G stay_out
drop in 1 
destring active stay_out, replace
gen year=year(StartDate)
collapse active stay_out, by(year)
tsset year
tsfill
ipolate active year, gen(iactive)
label variable iactive "Public Internationalism"
sort year
merge 1:1 year using `usafoundings'
keep if year>1991 & year<2021
twoway (line iactive year, c(l) yaxis(1) lwidth(medthick) legend(off)) (line count year, c(l) yaxis(2) xtitle("") lwidth(medthick) legend(off)), scheme(plotplain) scale(1.5)
*Manually label Foundings and Attitudes using Greyscale6 and Greyscale10
graph export "Figures/Figure3-2av2.pdf", as(pdf) replace

/*3.2b*/
clear
import excel "Data/NGOs_AllCountries", sheet("Sheet1") firstrow
keep if CountryTerritoryHQ=="USA"
gen count=1
collapse (sum) count, by(Founded)
label variable count "Foundings"
label variable Founded "Year"
rename Founded year
tempfile usafoundings
save `usafoundings'
clear
import excel "Data/us_foreignaid_greenbook.xlsx", sheet("Economic and Military Aid") firstrow
keep if AssistanceCategory=="Economic"
collapse (sum) ObligationsConstantDollars, by(FiscalYear)
rename FiscalYear year
merge 1:1 year using `usafoundings'
generate aid_billions=ObligationsConstantDollars/1000000000
label variable aid_billions "Billions of Constant 2017 Dollars"
keep if year>1991 & year<2021
twoway (line aid_billions year, c(l) yaxis(1) lwidth(medthick) legend(off)) (line count year, c(l) yaxis(2) xtitle("") lwidth(medthick) legend(off)), scheme(plotplain) scale(1.5)
*Manually label Foundings and Aid using Greyscale6 and Greyscale10
graph export "Figures/Figure3-2b.pdf", as(pdf) replace

/*Figure 3.4: American INGO Foundings by Sector, 1992-2019*/
clear
use "Data/PZ-1990-2019-Q.dta"
generate field=.
replace field=2 if nteefinal=="Q33"
replace field=3 if nteefinal=="Q35"
replace field=4 if nteefinal=="Q38"
replace field=5 if nteefinal=="Q39"
label define fieldgroups 2 "Relief" 3 "Democracy" 4 "Environment" 5 "Health"
label values field fieldgroups
drop if field==. 
sort ein fisyr
quietly by ein: gen dup = cond(_N==1, 0, _n)
drop if dup>1 & ein!=.
drop dup
sort name fisyr
quietly by name: gen dup = cond(_N==1, 0, _n)
drop if dup>1 
drop dup
destring ruledate, replace
drop if ruledate==. | ruledate<190000
generate foundingyear=int(ruledate/100)
destring fisyr, replace
generate founded=1
by field foundingyear, sort: generate foundings=sum(founded)
collapse (max) foundings, by(field foundingyear)
label variable foundings "Number"
generate year=foundingyear
sort year field
merge year field using "Data/yearlist_1990-2019.dta"
replace foundings=0 if _merge==2
sort field year
line foundings year if field==2 & (year>1991 & year<2020), title({bf: Humanitarianism}) name(relief) nodraw scale(1.5) xtitle("") scheme(plotplain) lwidth(medthick)
line foundings year if field==3 & (year>1991 & year<2020), title({bf: Civil Society & Democracy}) name(democracy) nodraw scale(1.5) xtitle("") scheme(plotplain) lwidth(medthick)
line foundings year if field==4 & (year>1991 & year<2020), title({bf: Conservation}) name(environment) nodraw scale(1.5) xtitle("") scheme(plotplain) lwidth(medthick)
line foundings year if field==5 & (year>1991 & year<2020), title({bf: Global Health}) name(health) nodraw scale(1.5) xtitle("") scheme(plotplain) lwidth(medthick)
graph combine environment democracy health relief
graph export "Figures/Figure3-4.pdf", as(pdf) replace
graph drop relief democracy environment health

/*Figure 3.5: American INGO Revenues by Sector, 1992-2019*/
clear
use "Data/PZ-1990-2019-Q.dta"
generate field=.
replace field=2 if nteefinal=="Q33"
replace field=3 if nteefinal=="Q35"
replace field=4 if nteefinal=="Q38"
replace field=5 if nteefinal=="Q39"
label define fieldgroups 2 "Relief" 3 "Democracy" 4 "Environment" 5 "Health"
label values field fieldgroups
drop if field==. 
sort ein fisyr
quietly by ein fisyr: gen dup = cond(_N==1, 0, _n)
sum dup
drop if dup>1 & ein!=.
drop dup
*Use calculator from https://www.usinflationcalculator.com/ for constant 2020 USD
generate totrev_inflation=.
replace totrev_inflation=totrev*1.012 if fisyr==2019
replace totrev_inflation=totrev*1.031 if fisyr==2018
replace totrev_inflation=totrev*1.056 if fisyr==2017
replace totrev_inflation=totrev*1.078 if fisyr==2016
replace totrev_inflation=totrev*1.092 if fisyr==2015
replace totrev_inflation=totrev*1.093 if fisyr==2014
replace totrev_inflation=totrev*1.111 if fisyr==2013
replace totrev_inflation=totrev*1.127 if fisyr==2012
replace totrev_inflation=totrev*1.151 if fisyr==2011
replace totrev_inflation=totrev*1.187 if fisyr==2010
replace totrev_inflation=totrev*1.206 if fisyr==2009
replace totrev_inflation=totrev*1.202 if fisyr==2008
replace totrev_inflation=totrev*1.248 if fisyr==2007
replace totrev_inflation=totrev*1.284 if fisyr==2006
replace totrev_inflation=totrev*1.325 if fisyr==2005
replace totrev_inflation=totrev*1.370 if fisyr==2004
replace totrev_inflation=totrev*1.407 if fisyr==2003
replace totrev_inflation=totrev*1.439 if fisyr==2002
replace totrev_inflation=totrev*1.462 if fisyr==2001
replace totrev_inflation=totrev*1.503 if fisyr==2000
replace totrev_inflation=totrev*1.553 if fisyr==1999
replace totrev_inflation=totrev*1.588 if fisyr==1998
replace totrev_inflation=totrev*1.613 if fisyr==1997
replace totrev_inflation=totrev*1.650 if fisyr==1996
replace totrev_inflation=totrev*1.698 if fisyr==1995
replace totrev_inflation=totrev*1.746 if fisyr==1994
replace totrev_inflation=totrev*1.791 if fisyr==1993
replace totrev_inflation=totrev*1.845 if fisyr==1992
replace totrev_inflation=totrev*1.900 if fisyr==1991
replace totrev_inflation=totrev*1.980 if fisyr==1990
destring fisyr, replace
collapse (sum) totrev_inflation, by(field fisyr)
generate constantmillions=(totrev_inflation/1000000)
label variable constantmillions "Dollars (Millions)"
sort field fisyr
line constantmillions fisyr if field==2 & (fisyr>1991 & fisyr<2020), title({bf: Humanitarianism}) name(relief) nodraw scale(1.5) xtitle("") yla(, format(%12.0fc)) scheme(plotplain) lwidth(medthick)
line constantmillions fisyr if field==3 & (fisyr>1991 & fisyr<2020), title({bf: Civil Society & Democracy}) name(democracy) nodraw scale(1.5) xtitle("") yla(, format(%12.0fc)) scheme(plotplain) lwidth(medthick)
line constantmillions fisyr if field==4 & (fisyr>1991 & fisyr<2020), title({bf: Conservation}) name(environment) nodraw scale(1.5) xtitle("") yla(, format(%12.0fc)) scheme(plotplain) lwidth(medthick)
line constantmillions fisyr if field==5 & (fisyr>1991 & fisyr<2020), title({bf: Global Health}) name(health) nodraw scale(1.5) xtitle("") yla(, format(%12.0fc)) scheme(plotplain) lwidth(medthick)
graph combine environment democracy health relief
graph export "Figures/Figure3-5.pdf", as(pdf) replace
graph drop relief democracy environment health

/*Figure 3.6: Explaining Variation in the Founding of American INGOs, 1992-2019*/
use "Data/PZ-1990-2019-Q.dta", clear
generate field=.
replace field=2 if nteefinal=="Q33"
replace field=3 if nteefinal=="Q35"
replace field=4 if nteefinal=="Q38"
replace field=5 if nteefinal=="Q39"
label define fieldgroups 2 "Relief" 3 "Democracy" 4 "Environment" 5 "Health"
label values field fieldgroups
drop if field==. 
sort ein fisyr
quietly by ein fisyr: gen dup = cond(_N==1, 0, _n)
sum dup
drop if dup>1 & ein!=.
drop dup
generate org=1
by field fisyr, sort: egen density=sum(org)
label variable density "number of active INGOs"
keep field fisyr density
generate density_sq=density*density
collapse (mean) density density_sq, by(field fisyr)
rename fisyr year
sort year field
merge year field using "Data/yearlist_1990-2019.dta"
replace density=0 if _merge==2
replace density_sq=0 if _merge==2
replace year=year+1 /*to lag density*/
drop if year>2019 | year<1992
drop _merge
sort field year
tempfile density
save `density'
use "Data/PZ-1990-2019-Q.dta", clear
generate field=.
replace field=2 if nteefinal=="Q33"
replace field=3 if nteefinal=="Q35"
replace field=4 if nteefinal=="Q38"
replace field=5 if nteefinal=="Q39"
label define fieldgroups 2 "Relief" 3 "Democracy" 4 "Environment" 5 "Health"
label values field fieldgroups
drop if field==. 
sort ein fisyr
quietly by ein: gen dup = cond(_N==1, 0, _n)
drop if dup>1 & ein!=.
drop dup
sort name fisyr
quietly by name: gen dup = cond(_N==1, 0, _n)
drop if dup>1 
drop dup
destring ruledate, replace
drop if ruledate==. | ruledate<190000
generate foundingyear=int(ruledate/100)
destring fisyr, replace
generate founded=1
by field foundingyear, sort: generate foundings=sum(founded)
collapse (max) foundings, by(field foundingyear)
label variable foundings "Number"
generate year=foundingyear
sort year field
merge year field using "Data/yearlist_1990-2019.dta"
replace foundings=0 if _merge==2
drop _merge
sort field year
merge 1:1 field year using `density'
drop if _merge==1
drop _merge
sort field year
merge field year using "Data/US Aid by Sector Year Combined.dta"
drop _merge
sort field year 
merge field year using "Data/congress.dta"
drop _merge
sort field year 
merge field year using "Data/media.dta"
drop _merge
egen stddensity=std(density)
egen stddensity_sq=std(density_sq)
generate lnaid=log(total_aid+1)
label variable stddensity "Density"
label variable stddensity_sq "Density Squared"
label variable lnaid "U.S. Aid"
label variable lnmedia "Media Attention"
label variable lncongress "Policy Attention"
drop if year<1992
reg foundings lnaid lnmedia lncongress, vce(cluster field)
estimates store Baseline
reg foundings lnaid lnmedia lncongress stddensity stddensity_sq, vce(cluster field)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge) format(%12.0fc)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Figure3-6.pdf", as(pdf) replace

/*Figure 3.7: Explaining Variation in American INGO Deaths, 1992-2016*/
use "Data/PZ-1990-2019-Q.dta", clear
generate field=.
replace field=2 if nteefinal=="Q33"
replace field=3 if nteefinal=="Q35"
replace field=4 if nteefinal=="Q38"
replace field=5 if nteefinal=="Q39"
label define fieldgroups 2 "Relief" 3 "Democracy" 4 "Environment" 5 "Health"
label values field fieldgroups
drop if field==. 
sort ein fisyr
quietly by ein fisyr: gen dup = cond(_N==1, 0, _n)
sum dup
drop if dup>1 & ein!=.
drop dup
drop if ein==.
drop if fisyr<1992 | fisyr>2019
sort ein fisyr
by ein, sort: gen years=fisyr[_n+1]-fisyr
generate fail=0
replace fail=1 if years>2
by ein, sort: generate totalobs=_N
sort ein fisyr
by ein, sort: generate id=_n
replace fail=1 if totalobs==id & fisyr<2017
label variable fail "=1 if the organization fails that year"
generate foundingyear=int(ruledate/100)
destring fisyr, replace
generate age=fisyr-foundingyear+1
replace age=. if age>1900
label variable age "organizational age in years (IRS ruling = age 1)"
keep ein fisyr field fail totrev age
replace totrev=0 if totrev<0
gen lnrev=log(totrev+1)
rename fisyr year
sort ein year
by ein: gen lastobs = _n == _N
replace fail=0 if fail==1 & lastobs==0
sort field year
tempfile failures
save `failures'
use "Data/PZ-1990-2019-Q.dta", clear
generate field=.
replace field=2 if nteefinal=="Q33"
replace field=3 if nteefinal=="Q35"
replace field=4 if nteefinal=="Q38"
replace field=5 if nteefinal=="Q39"
label define fieldgroups 2 "Relief" 3 "Democracy" 4 "Environment" 5 "Health"
label values field fieldgroups
drop if field==. 
sort ein fisyr
quietly by ein fisyr: gen dup = cond(_N==1, 0, _n)
sum dup
drop if dup>1 & ein!=.
drop dup
generate org=1
by field fisyr, sort: egen density=sum(org)
label variable density "number of active INGOs"
keep field fisyr density
generate density_sq=density*density
collapse (mean) density density_sq, by(field fisyr)
rename fisyr year
sort year field
merge year field using "Data/yearlist_1990-2019.dta"
replace density=0 if _merge==2
replace density_sq=0 if _merge==2
replace year=year+1 /*to lag density*/
drop if year>2019
drop _merge
sort field year
merge field year using `failures'
egen stddensity=std(density)
egen stddensity_sq=std(density_sq)
egen stdage=std(age)
label variable stddensity "Density"
label variable stddensity_sq "Density Squared"
label variable stdage "INGO Age"
label variable lnrev "INGO Revenue"
reg fail lnrev stdage i.year if year<2017, vce(cluster field)
estimates store Baseline
reg fail lnrev stdage stddensity stddensity_sq i.year if year<2017, vce(cluster field)
estimates store Full
coefplot Baseline Full, keep(*lnrev *stdage *stddensity *stddensity_sq) xline(0) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Figure3-7.pdf", as(pdf) replace
